package com.binguo.api.schedule;

import com.binguo.api.huawei.imagesearch.model.HuaweiProduct;
import com.binguo.common.util.LoggerUtil;
import com.binguo.model.mapper.*;
import com.binguo.model.model.*;
import com.binguo.model.repository.*;
import com.sun.tools.corba.se.idl.toJavaPortable.InterfaceGen;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DataLoadService implements InitializingBean {

    @Autowired
    private CashierDeskRepository cashierDeskRepository;

    @Autowired
    private CustomerGroupBranchRepository customerGroupBranchRepository;
    @Autowired
    private ProductRepository productRepository;
    @Autowired
    private ProductYpRefRepository productYpRefRepository;
    @Autowired
    private ProductYpRepository productYpRepository;
    @Autowired
    private ProductYpTypeRepository productYpTypeRepository;
    @Autowired
    private ShopProductYpTypeRefRepository shopProductYpTypeRefRepository;
    @Autowired
    private HuaweiUrlRepository huaweiUrlRepository;

    @Resource
    private ShopBranchBaiduIdMapper shopBranchBaiduIdMapper;
    @Resource
    private CashierDeskMapper cashierDeskMapper;
    @Resource
    private ShopBaiduUrlCurMapper shopBaiduUrlCurMapper;
    @Resource
    private ShopProductRefMapper shopProductRefMapper;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private BranchMapper branchMapper;

    @Resource
    private ShopPackageDiscountMapper shopPackageDiscountMapper;

    @Resource
    private ShopPayConfigMapper shopPayConfigMapper;
    @Resource
    private ShopBowlAreaMapper shopBowlAreaMapper;
    @Resource
    private ShopPayCashierDeskConfigMapper shopPayCashierDeskConfigMapper;
    @Resource
    private ShopRecTypeConfigMapper shopRecTypeConfigMapper;
    @Resource
    private RecipMapper recipMapper;
    @Resource
    private NormalConfigMapper normalConfigMapper;
    @Resource
    private ShopYizhifuKeyRepository shopYizhifuKeyRepository;

    public static Map<Integer, Integer> cashierDeskBranchMap = null;
    public static Map<Integer, String> cashierDeskDeviceIdMap = null;
    public static Map<Integer, String> cashierDeskStatnumMap = null;
    public static Map<Integer, Product> productMap = null;
    public static Map<Integer, List<Product>> shopProductListMap = null;
    public static Map<Integer, String> productNameMap = null;
    public static Map<Integer, List<Integer>> branchCustomerGroupListMap = null;
    public static Map<Integer, List<ProductYpRef>> shopYpProductListMap = null;
    public static Map<Integer, List<ShopBaiduUrlCur>> shopBaiduUrlCurListMap = null;
    public static Map<Integer, List<String>> shopYpProductBaiduTagNameListMap = null;
    public static Map<Integer, List<String>> shopYpProductSearchCodeListMap = null;
    public static Map<Integer, Map<String, Product>> shopSearchCodeProductMap = null;
    public static Map<Integer, ShopYizhifuKey> shopYiZhiFuMap = null;

    public static Map<String, Integer> deviceBaiduShopIdMap = null;
    public static Map<String, CashierDesk> deviceBranchIdMap = null;
    public static Map<Integer, String> shopLocalRecUrlmap = null;
    public static Map<Integer, Double> shopLocalRecScoreMap = null;
    public static Map<Integer, Integer> shopProductRefMap = null;
    public static Map<Integer, Shop> shopMap = null;
    public static Map<String, Shop> shopAppIdMap = null;
    public static Map<String, Shop> appIdShopMap = null;
    public static Map<Integer, List<ShopPackageDiscount>> shopPackageDiscountMap = null;
    public static Map<Integer, List<ShopPayConfig>> shopPayConfigListMap = null;
    public static Map<Integer, List<ShopPayCashierDeskConfig>> shopPayCashierDeskConfigListMap = null;
    public static Map<Integer, Map<Integer, Integer>> customerIdConsumeCountMap = new HashMap<>();
    public static Map<Integer, Map<Integer, Integer>> shopBranchZiDingYiProductIdMap = new HashMap<>();

    public static Map<Integer, List<Branch>> shopBranchListMap;
    public static Map<Integer, Branch> branchListMap;
    public static Map<Integer, Integer> shopRecOpenMap;
    public static Map<Integer, Integer> shopBowlMinAreaMap;
    public static Map<Integer, Integer> shopBowlMaxAreaMap;
    public static Map<Integer, String> shopBowlRecIpMap;
    public static Map<Integer, String> shopRecUrlMap = null;
    public static Map<String, Integer> shopBranchBaiduIdListMap;
    public static Map<String, String> shopBranchBaiduAppIdListMap;
    public static Map<Integer, Map<String, ProductYp>> shopProductYpSearchCodeMap;

    public static List<Integer> shopIdList = null;
    public static String huaweiUrl = null;
    public static double huaweiScore = 0.900;
    public static int useBaidu = 0;
    public static String bowlRecUrl = null;
    public static String bowlRecUrl2 = null;
    public static String bowlRecUrl3 = null;
    public static String recip = null;
    public static Map<Integer, String> mankebaoConfigMap = new HashMap<>();

    public static Integer getBranchId(String deviceId, Integer shopId) {
        if (deviceBranchIdMap != null && StringUtils.hasText(deviceId)) {
            CashierDesk cashierDesk = deviceBranchIdMap.get(deviceId);
            if (cashierDesk != null && cashierDesk.getShopid().intValue() == shopId.intValue()) {
                return cashierDesk.getBranchid();
            }
        }
        return null;
    }

    public static ProductYpRef getProductYpRef(String baiduName, Integer shopId, Integer branchId) {
        if (shopYpProductListMap != null) {

            List<ProductYpRef> tempList = shopYpProductListMap.get(shopId);

            if (tempList != null) {
                if (branchId == null || branchId == 0) {
                    for (ProductYpRef productYpRef : tempList) {
                        if (productYpRef.getBaiduTagName() != null && productYpRef.getBaiduTagName().equals(baiduName)) {
                            return productYpRef;
                        }
                    }
                }else {
                    ProductYpRef tempResult = null;
                    for (ProductYpRef productYpRef : tempList) {
                        if (productYpRef.getBranchId() == null || productYpRef.getBranchId() == 0) {
                            if (productYpRef.getBaiduTagName() != null && productYpRef.getBaiduTagName().equals(baiduName)) {
                                tempResult = productYpRef;
                            }
                        }else {
                            if (productYpRef.getBranchId().intValue() == branchId.intValue()) {
                                if (productYpRef.getBaiduTagName() != null && productYpRef.getBaiduTagName().equals(baiduName)) {
                                    return productYpRef;
                                }
                            }
                        }
                    }

                    return tempResult;
                }

            }
        }

        return null;
    }
    public static ProductYpRef getProductYpRefBySearchCode(String searchCode, Integer shopId, Integer branchId) {
        if (shopYpProductListMap != null) {

            List<ProductYpRef> tempList = shopYpProductListMap.get(shopId);

            if (tempList != null) {
                if (branchId == null || branchId == 0) {
                    for (ProductYpRef productYpRef : tempList) {
                        if (productYpRef.getSearchCode().equals(searchCode)) {
                            return productYpRef;
                        }
                    }
                }else {
                    ProductYpRef tempResult = null;
                    for (ProductYpRef productYpRef : tempList) {
                        if (productYpRef.getBranchId() == null || productYpRef.getBranchId() == 0) {
                            if (productYpRef.getSearchCode().equals(searchCode)) {
                                tempResult = productYpRef;
                            }
                        }else {
                            if (productYpRef.getBranchId().intValue() == branchId.intValue()) {
                                if (productYpRef.getSearchCode().equals(searchCode)) {
                                    return productYpRef;
                                }
                            }
                        }
                    }

                    return tempResult;
                }

            }
        }

        return null;
    }
    /**
     * 每十分钟更新一次
     * @throws Exception
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void loadData() throws Exception{
        LoggerUtil.console.info("begin load data");
        List<NormalConfig> normalConfigList = normalConfigMapper.selectByExample(new NormalConfigExample());
        if(normalConfigList != null){
            for(NormalConfig normalConfig : normalConfigList){
                if (normalConfig.getShopId() != null) {
                    mankebaoConfigMap.put(normalConfig.getShopId(),normalConfig.getValue());
                }
            }
        }
        List<Recip> recipList = recipMapper.selectAll();
        if (recipList != null && recipList.size() > 0) {
            recip = recipList.get(0).getAddress();

            Map<Integer, String> tempShopBowlRecIpMap = null;
            for (Recip recip : recipList) {
                if (recip.getIpType() != null && recip.getIpType().intValue() > 1) {
                    if (tempShopBowlRecIpMap == null) {
                        tempShopBowlRecIpMap = new HashMap<>();
                    }

                    tempShopBowlRecIpMap.put(recip.getIpType(), recip.getAddress());
                }
            }

            shopBowlRecIpMap = tempShopBowlRecIpMap;
        }

        List<ShopBowlArea> shopBowlAreaList = shopBowlAreaMapper.selectAll();
        if (shopBowlAreaList != null && shopBowlAreaList.size() > 0) {
            shopBowlMinAreaMap = shopBowlAreaList.stream().collect(Collectors.toMap(ShopBowlArea::getShopId, ShopBowlArea::getMinArea));
            shopBowlMaxAreaMap = shopBowlAreaList.stream().collect(Collectors.toMap(ShopBowlArea::getShopId, ShopBowlArea::getMaxArea));
        }

        List<ShopRecTypeConfig> shopRecTypeConfigList = shopRecTypeConfigMapper.selectAll();

        if (shopRecTypeConfigList != null && shopRecTypeConfigList.size() > 0) {
            shopRecOpenMap = shopRecTypeConfigList.stream().collect(Collectors.toMap(ShopRecTypeConfig::getShopId, ShopRecTypeConfig::getOpened));
        }

        Condition productYpTypeCondition0 = new Condition(ProductYpType.class);
        productYpTypeCondition0.or().andIsNotNull("shopId");
        List<ProductYpType> productYpTypeList0 = productYpTypeRepository.findByCondition(productYpTypeCondition0);
        if (productYpTypeList0 != null && productYpTypeList0.size() > 0) {
            shopRecUrlMap = productYpTypeList0.stream()
                    .collect(Collectors.toMap(ProductYpType::getShopId, ProductYpType::getRecUrl));

        }

        Condition productYpTypeCondition = new Condition(ProductYpType.class);
        productYpTypeCondition.or().andEqualTo("ypType", 1);
        List<ProductYpType> productYpTypeList = productYpTypeRepository.findByCondition(productYpTypeCondition);
        if (productYpTypeList != null && productYpTypeList.size() > 0) {
            bowlRecUrl = productYpTypeList.get(0).getRecUrl();
        }

        Condition productYpTypeCondition2 = new Condition(ProductYpType.class);
        productYpTypeCondition2.or().andEqualTo("ypType", 11);
        List<ProductYpType> productYpTypeList2 = productYpTypeRepository.findByCondition(productYpTypeCondition2);
        if (productYpTypeList2 != null && productYpTypeList2.size() > 0) {
            bowlRecUrl2 = productYpTypeList2.get(0).getRecUrl();
        }

        Condition productYpTypeCondition3 = new Condition(ProductYpType.class);
        productYpTypeCondition3.or().andEqualTo("ypType", 111);
        List<ProductYpType> productYpTypeList3 = productYpTypeRepository.findByCondition(productYpTypeCondition3);
        if (productYpTypeList3 != null && productYpTypeList3.size() > 0) {
            bowlRecUrl3 = productYpTypeList3.get(0).getRecUrl();
        }

        List<ShopYizhifuKey> keyList = shopYizhifuKeyRepository.findAll();

        if (keyList != null && keyList.size() > 0) {
            shopYiZhiFuMap = new HashMap<>();
            for (ShopYizhifuKey key : keyList){
                shopYiZhiFuMap.put(key.getShopId(),key);
            }
        }

        List<Branch> branchList = branchMapper.selectAll();
        if (productYpTypeList != null && productYpTypeList.size() > 0) {
            Map<Integer, List<Branch>> tempShopBranchListMap = new HashMap<>();

            Map<Integer, Branch> tempBranchMap = new HashMap<>();
            for (Branch branch : branchList) {
                List<Branch> branches = tempShopBranchListMap.get(branch.getShopId());

                if (branches == null) {
                    branches = new ArrayList<>();
                }
                branches.add(branch);

                tempShopBranchListMap.put(branch.getShopId(), branches);

                if (tempBranchMap.get(branch.getBranchId()) == null) {
                    tempBranchMap.put(branch.getBranchId(), branch);
                }
            }

            branchListMap = tempBranchMap;
            shopBranchListMap = tempShopBranchListMap;
        }

        List<ShopPayConfig> shopPayConfigList = shopPayConfigMapper.selectAll();
        if (shopPayConfigList != null && shopPayConfigList.size() > 0) {
            Map<Integer, List<ShopPayConfig>> shopPayConfigMap = new HashMap<>();
            for (ShopPayConfig shopPayConfig : shopPayConfigList) {
                List<ShopPayConfig> payConfigList = shopPayConfigMap.get(shopPayConfig.getShopId());

                if (payConfigList == null) {
                    payConfigList = new ArrayList<>();
                }
                payConfigList.add(shopPayConfig);
                shopPayConfigMap.put(shopPayConfig.getShopId(), payConfigList);
            }
            shopPayConfigListMap = shopPayConfigMap;
        }

        List<ShopPayCashierDeskConfig> shopPayCashierDeskConfigList = shopPayCashierDeskConfigMapper.selectAll();

        if (shopPayCashierDeskConfigList != null && shopPayCashierDeskConfigList.size() > 0) {
            Map<Integer, List<ShopPayCashierDeskConfig>> shopCashiDeskIdConsumeCountMap = new HashMap<>();
            for (ShopPayCashierDeskConfig shopPayCashierDeskConfig : shopPayCashierDeskConfigList) {
                List<ShopPayCashierDeskConfig> shopPayCashierDeskConfigs =
                        shopCashiDeskIdConsumeCountMap.get(shopPayCashierDeskConfig.getShopId());

                if (shopPayCashierDeskConfigs == null) {
                    shopPayCashierDeskConfigs = new ArrayList<>();
                }
                shopPayCashierDeskConfigs.add(shopPayCashierDeskConfig);

                shopCashiDeskIdConsumeCountMap.put(shopPayCashierDeskConfig.getShopId(), shopPayCashierDeskConfigs);
            }
            shopPayCashierDeskConfigListMap = shopCashiDeskIdConsumeCountMap;
        }

        Condition shopPackageDiscountCondition = new Condition(ShopPackageDiscount.class);
        shopPackageDiscountCondition.or().andEqualTo("type", 1);

        List<ShopPackageDiscount> shopPackageDiscountList = shopPackageDiscountMapper.selectByCondition(shopPackageDiscountCondition);

        if (shopPackageDiscountList != null && shopPackageDiscountList.size() > 0) {
            Map<Integer, List<ShopPackageDiscount>> tempShopPackageDiscountMap = new HashMap<>();
            for (ShopPackageDiscount shopPackageDiscount : shopPackageDiscountList) {


                List<ShopPackageDiscount> packageDiscountList =
                        tempShopPackageDiscountMap.get(shopPackageDiscount.getShopid());

                if (packageDiscountList == null) {
                    packageDiscountList = new ArrayList<>();
                }

                packageDiscountList.add(shopPackageDiscount);

                tempShopPackageDiscountMap.put(shopPackageDiscount.getShopid(), packageDiscountList);
            }

            shopPackageDiscountMap = tempShopPackageDiscountMap;
        }


        List<Shop> shopList = shopMapper.selectAll();
        shopMap = shopList.stream().collect(Collectors.toMap(Shop::getId, s->s));
        shopAppIdMap = shopList.stream().collect(Collectors.toMap(Shop::getAppId, s->s));

        Map<String, Shop> tempAppIdShopMap = new HashMap<>();
        for (Shop shop : shopList) {
            if (StringUtils.hasText(shop.getAppId())) {
                tempAppIdShopMap.put(shop.getAppId(), shop);
            }
        }
        appIdShopMap = tempAppIdShopMap;

        List<ShopProductRef> shopProductRefList = shopProductRefMapper.selectAll();

        if (shopProductRefList != null && shopProductRefList.size() > 0) {

            Map<Integer, Integer> temp = new HashMap<>();
            for (ShopProductRef shopProductRef : shopProductRefList) {
                temp.put(shopProductRef.getShopId(), shopProductRef.getRefShopId());
                temp.put(shopProductRef.getRefShopId(), shopProductRef.getShopId());
            }

            shopProductRefMap = temp;

        }

        List<ShopBaiduUrlCur> shopBaiduUrlCurList = shopBaiduUrlCurMapper.selectAll();

        if (shopBaiduUrlCurList != null && shopBaiduUrlCurList.size() > 0) {
            Map<Integer, List<ShopBaiduUrlCur>> temp = new HashMap<>();

            Map<String, Integer> tempShopBranchBaiduIdListMap = new HashMap<>();
            Map<String, String> tempShopBranchBaiduAppIdListMap = new HashMap<>();

            for (ShopBaiduUrlCur shopBaiduUrlCur : shopBaiduUrlCurList) {
                List<ShopBaiduUrlCur> list = temp.get(shopBaiduUrlCur.getShopId());
                if (list == null) {
                    list = new ArrayList<>();
                }
                list.add(shopBaiduUrlCur);
                temp.put(shopBaiduUrlCur.getShopId(), list);

                Integer count = tempShopBranchBaiduIdListMap.get(shopBaiduUrlCur.getShopId() + "_" + shopBaiduUrlCur.getBranchId());

                if (count == null) {
                    count = 1;
                }else {
                    count++;
                }
                tempShopBranchBaiduIdListMap.put(shopBaiduUrlCur.getShopId() + "_" + shopBaiduUrlCur.getBranchId(), count);

                tempShopBranchBaiduAppIdListMap.put(shopBaiduUrlCur.getShopId() + "_" + shopBaiduUrlCur.getBranchId() + count, shopBaiduUrlCur.getAppid());
            }
            shopBaiduUrlCurListMap = temp;
            shopBranchBaiduIdListMap = tempShopBranchBaiduIdListMap;
            shopBranchBaiduAppIdListMap = tempShopBranchBaiduAppIdListMap;
        }

        List<ProductYp> productYpList = productYpRepository.findAll();
        Map<Integer, Map<String, ProductYp>> productYpTypeMap = new HashMap<>();
        if (productYpList != null && productYpList.size() > 0) {
            for (ProductYp productYp : productYpList) {
                Map<String, ProductYp> searchCodeProductYpMap = productYpTypeMap.get(productYp.getProductType());
                if (searchCodeProductYpMap == null) {
                    searchCodeProductYpMap = new HashMap<>();
                }

                searchCodeProductYpMap.put(productYp.getSearchCode(), productYp);

                productYpTypeMap.put(productYp.getProductType(), searchCodeProductYpMap);
            }
        }

        List<ShopProductYpTypeRef> shopProductYpTypeRefList = shopProductYpTypeRefRepository.findAll();

        if (shopProductYpTypeRefList != null && shopProductYpTypeRefList.size() > 0) {
            Map<Integer, String> tempShopLocalRecUrlmap = new HashMap<>();
            Map<Integer, Double> tempShopLocalRecScoreMap = new HashMap<>();

            Map<Integer, Map<String, ProductYp>> tempShopProductYpSearchCodeMap = new HashMap<>();

            for (ShopProductYpTypeRef shopProductYpTypeRef : shopProductYpTypeRefList) {
                ProductYpType productYpType = productYpTypeRepository.findById(shopProductYpTypeRef.getProductTypeId());

                tempShopLocalRecUrlmap.put(shopProductYpTypeRef.getShopId(), productYpType.getRecUrl());


                Map<String, ProductYp> searchCodeProductYpMap = productYpTypeMap.get(shopProductYpTypeRef.getProductTypeId());

                tempShopProductYpSearchCodeMap.put(shopProductYpTypeRef.getShopId(), searchCodeProductYpMap);


                if (productYpType.getScore() != null) {
                    tempShopLocalRecScoreMap.put(shopProductYpTypeRef.getShopId(), productYpType.getScore());
                }
            }
            shopProductYpSearchCodeMap = tempShopProductYpSearchCodeMap;
            shopLocalRecUrlmap = tempShopLocalRecUrlmap;
            shopLocalRecScoreMap = tempShopLocalRecScoreMap;
        }

        List<CashierDesk> cashierDeskList = cashierDeskMapper.selectAll();

        Map<String, List<CashierDesk>> cashierDeskMap = new HashMap<>();
        if (cashierDeskList != null && cashierDeskList.size() > 0) {
            Map<String, CashierDesk> tempDeviceBranchIdMap = new HashMap<>();
            cashierDeskList.stream().forEach(cashierDesk -> {
                String key = cashierDesk.getShopid() + "_" + cashierDesk.getBranchid();
                List<CashierDesk> tempList = cashierDeskMap.get(key);
                if (tempList == null) {
                    tempList = new ArrayList<>();
                }

                tempDeviceBranchIdMap.put(cashierDesk.getCcode(), cashierDesk);

                tempList.add(cashierDesk);
                cashierDeskMap.put(key, tempList);
            });

            deviceBranchIdMap = tempDeviceBranchIdMap;
        }

        List<ShopBranchBaiduId> shopBranchBaiduIdList = shopBranchBaiduIdMapper.selectAll();

        if (shopBranchBaiduIdList != null && shopBranchBaiduIdList.size() > 0) {
            deviceBaiduShopIdMap = new HashMap<>();
            shopBranchBaiduIdList.stream().forEach(shopBranchBaiduId -> {
                List<CashierDesk> tempList = cashierDeskMap.get(shopBranchBaiduId.getShopId() +
                        "_" + shopBranchBaiduId.getBranchId());
                if (tempList != null && tempList.size() > 0) {
                    for (CashierDesk cashierDesk : tempList) {
                        deviceBaiduShopIdMap.put(cashierDesk.getCcode(), shopBranchBaiduId.getBaiduShopId());
                    }
                }
            });
        }

        List<HuaweiUrl> huaweiUrlList = huaweiUrlRepository.findAll();
        if (huaweiUrlList != null && huaweiUrlList.size() > 0) {
            huaweiUrl = huaweiUrlList.get(0).getHuaweiUrl();
            huaweiScore = huaweiUrlList.get(0).getScore();

            if (huaweiUrlList.get(0).getUseBaidu() != null) {
                useBaidu = huaweiUrlList.get(0).getUseBaidu();
            }
        }

        List<ProductYpRef> productYpRefList =
                productYpRefRepository.findAll();

        if (productYpRefList != null && productYpRefList.size() > 0) {
            shopYpProductListMap = new HashMap<>();

            Map<Integer, List<String>> tempShopYpProductBaiduTagNameListMap = new HashMap<>();
            Map<Integer, List<String>> tempShopYpProductSearchCodeListMap = new HashMap<>();
            for (ProductYpRef productYpRef : productYpRefList) {
                List<ProductYpRef> tempList = shopYpProductListMap.get(productYpRef.getShopId());

                if (tempList == null) {
                    tempList = new ArrayList<>();
                }

                tempList.add(productYpRef);
                shopYpProductListMap.put(productYpRef.getShopId(), tempList);


                List<String> shopBaiduNameList = tempShopYpProductBaiduTagNameListMap.get(productYpRef.getShopId());
                List<String> shopSearchCodeList = tempShopYpProductSearchCodeListMap.get(productYpRef.getShopId());
                if (shopBaiduNameList == null) {
                    shopBaiduNameList = new ArrayList<>();
                }
                if (shopSearchCodeList == null) {
                    shopSearchCodeList = new ArrayList<>();
                }
                shopBaiduNameList.add(productYpRef.getBaiduTagName());
                shopSearchCodeList.add(productYpRef.getSearchCode());

                tempShopYpProductBaiduTagNameListMap.put(productYpRef.getShopId(), shopBaiduNameList);
                tempShopYpProductSearchCodeListMap.put(productYpRef.getShopId(), shopSearchCodeList);
            }
            shopYpProductBaiduTagNameListMap = tempShopYpProductBaiduTagNameListMap;

            shopYpProductSearchCodeListMap = tempShopYpProductSearchCodeListMap;
        }


        if (cashierDeskList != null && cashierDeskList.size() > 0) {
            cashierDeskBranchMap =
                    cashierDeskList.stream().collect(Collectors.toMap(CashierDesk::getId, CashierDesk::getBranchid));


            cashierDeskDeviceIdMap =
                    cashierDeskList.stream().filter(item -> item.getCcode() != null).collect(Collectors.toMap(CashierDesk::getId, CashierDesk::getCcode));


//            cashierDeskStatnumMap =
//                    cashierDeskList.stream().collect(Collectors.toMap(CashierDesk::getId, CashierDesk::getStatnum));

        }


        List<CustomerGroupBranch> customerGroupBranchList = customerGroupBranchRepository.findAll();

        if (customerGroupBranchList != null && customerGroupBranchList.size() > 0) {
            List<Integer> tempShopIdList = new ArrayList<>();
            branchCustomerGroupListMap = new HashMap<>();
            for (CustomerGroupBranch customerGroupBranch : customerGroupBranchList) {
                List<Integer> customerGroupList =
                        branchCustomerGroupListMap.get(customerGroupBranch.getBranchId());
                if (customerGroupList == null) {
                    customerGroupList = new ArrayList<>();
                    branchCustomerGroupListMap.put(customerGroupBranch.getBranchId(), customerGroupList);
                }

                customerGroupList.add(customerGroupBranch.getCustomerGroupId());

                if (!tempShopIdList.contains(customerGroupBranch.getShopId())) {
                    tempShopIdList.add(customerGroupBranch.getShopId());
                }

            }

            shopIdList = tempShopIdList;
        }

        Condition productCondition = new Condition(Product.class);
        productCondition.or().andEqualTo("deleted", false);
        List<Product> productList = productRepository.findByCondition(productCondition);

        if (productList != null && productList.size() > 0) {

            Map<Integer, Product> tempMap = new HashMap<>();
            Map<Integer, String> tempProductNameMap = new HashMap<>();
            Map<Integer, List<Product>> tempShopProductListMap = new HashMap<>();

            Map<Integer, Map<String, Product>> tempShopSearchCodeProductMap = new HashMap<>();

            Map<Integer, Map<Integer, Integer>> tempShopBranchZiDingYiProductIdMap = new HashMap<>();
            for (Product product : productList) {
                tempMap.put(product.getId(), product);
                tempProductNameMap.put(product.getId(), product.getName());

                Map<String, Product> searchCodeProductMap = tempShopSearchCodeProductMap.get(product.getShopid());

                if (searchCodeProductMap == null) {
                    searchCodeProductMap = new HashMap<>();
                    tempShopSearchCodeProductMap.put(product.getShopid(), searchCodeProductMap);
                }

                if (StringUtils.hasText(product.getSearchcode())) {
                    searchCodeProductMap.put(product.getSearchcode(), product);
                }

                List<Product> shopProductList = tempShopProductListMap.get(product.getShopid());
                if (shopProductList == null) {
                    shopProductList = new ArrayList<>();
                }
                shopProductList.add(product);
                tempShopProductListMap.put(product.getShopid(), shopProductList);

                if ("-5".equals(product.getSearchcode())) {
                    Map<Integer, Integer> branchIdProductIdMap = tempShopBranchZiDingYiProductIdMap.get(product.getShopid());

                    if (branchIdProductIdMap == null) {
                        branchIdProductIdMap = new HashMap<>();
                    }
                    if (product.getStoreid() != null) {
                        branchIdProductIdMap.put(product.getStoreid(), product.getId());

                        tempShopBranchZiDingYiProductIdMap.put(product.getShopid(), branchIdProductIdMap);
                    }

                }

            }

            productMap = tempMap;
            productNameMap = tempProductNameMap;
            shopSearchCodeProductMap = tempShopSearchCodeProductMap;
            shopProductListMap = tempShopProductListMap;
            shopBranchZiDingYiProductIdMap = tempShopBranchZiDingYiProductIdMap;
        } else {
            productMap = null;
            productNameMap = null;
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        loadData();

        bashCommand("sh /data/startup/starts.sh");
    }

    public static String bashCommand(String command) {
        Process process = null;
        String stringBack = null;
        List<String> processList = new ArrayList<String>();
        try {
            process = Runtime.getRuntime().exec(command);
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line = "";
            while ((line = input.readLine()) != null) {
                processList.add(line);
            }
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (String line : processList) {
            stringBack += line;
            stringBack +="\n";
        }
        return stringBack;
    }
}
